package com.example.wendao.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.paging.DataSource;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;


public class PageUtils<T> {

    private static final int PAGE_SIZE = 5;

    private static final boolean ENABLE_PLACEHOLDERS = false;

    public static final int ITEM_TYPE_HEADER = 1;

    public static final int ITEM_TYPE_FOOTER = 2;


    public LiveData<PagedList<T>> buildLivePagedList(DataSource.Factory<Integer, T> factory){
        return new LivePagedListBuilder<>(
                factory, new PagedList.Config.Builder()
                        .setPageSize(PAGE_SIZE)
                        .setEnablePlaceholders(ENABLE_PLACEHOLDERS)
                        .setInitialLoadSizeHint(PAGE_SIZE)
                        .build())
                .build();
    }

    public static void initRecyclerView(RecyclerView view, RecyclerView.Adapter adapter, RecyclerView.LayoutManager layoutManager){
        view.setLayoutManager(layoutManager);
        view.setAdapter(adapter);
    }

    public static View inflateItemView(ViewGroup viewGroup, int itemId){
        return LayoutInflater
                .from(viewGroup.getContext())
                .inflate(itemId,
                        viewGroup,
                        false);
    }

}
